﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntitiesModel;


namespace ITMan
{
    public partial class FormQueryAuthFall : Form
    {
        public FormQueryAuthFall()
        {
            InitializeComponent();
        }

        public BindingSource bindingSource = new BindingSource();

        private void FormQueryAuthFall_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;

            var dbConText = new EadEntitiesModel();
            IQueryable<VW_AUTH_FALL> authFalls = from i in dbConText.VW_AUTH_FALLs 
                                                 where i.OCCURE_TIME >= DateTime.Today
                                                 orderby i.OCCURE_TIME descending 
                                                 select i ;
            if (!authFalls.Any())
            {
                MessageBox.Show("本日无认证失败数据！");
            }
            else
            {
                bindingSource.DataSource = authFalls;
                dataGridView1.DataSource = bindingSource;
                dataGridView1.Columns["ID"].Visible = false;
                dataGridView1.Columns["USER_NAME"].HeaderText = "用户名";
                dataGridView1.Columns["LOGIN_NAME"].HeaderText = "登录名";
                dataGridView1.Columns["IP"].HeaderText = "IP地址";
                dataGridView1.Columns["MAC_ADDRESS"].HeaderText = "MAC地址";
                dataGridView1.Columns["OCCURE_TIME"].HeaderText = "时间";
                dataGridView1.Columns["SERVER_REMARK"].HeaderText = "信息";
            }
        }

        private void btQueryMac_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            IQueryable<VW_AUTH_FALL> authFalls=null;

            var dbConText = new EadEntitiesModel();

            if (checkBoxToday.Checked && !string.IsNullOrEmpty(tbMac.Text))
            {
               authFalls = from i in dbConText.VW_AUTH_FALLs
                                                     where i.MAC_ADDRESS.Replace(":","") == tbMac.Text.Replace("-","").Replace(":","").ToUpper() && i.OCCURE_TIME >= DateTime.Today
                                                     orderby i.OCCURE_TIME descending 
                                                     select i;
            }
            else if (checkBoxToday.Checked && string.IsNullOrEmpty(tbMac.Text))
            {
                authFalls = from i in dbConText.VW_AUTH_FALLs
                            where i.OCCURE_TIME >= DateTime.Today
                            orderby i.OCCURE_TIME descending
                            select i;
            }
            else if (!checkBoxToday.Checked && !string.IsNullOrEmpty(tbMac.Text))
            {
                authFalls = from i in dbConText.VW_AUTH_FALLs
                            where i.MAC_ADDRESS == tbMac.Text
                            orderby i.OCCURE_TIME descending
                            select i;
            }
            else if (!checkBoxToday.Checked && string.IsNullOrEmpty(tbMac.Text))
            {
                authFalls = from i in dbConText.VW_AUTH_FALLs
                            //where i.MAC_ADDRESS == tbMac.Text
                            orderby i.OCCURE_TIME descending
                            select i;
            }


            if (!authFalls.Any())
            {
                MessageBox.Show("无数据！");
            }
            else
            {
                bindingSource.DataSource = authFalls;
                dataGridView1.DataSource = bindingSource;
                dataGridView1.Columns["ID"].Visible = false;
                dataGridView1.Columns["USER_NAME"].HeaderText = "用户名";
                dataGridView1.Columns["LOGIN_NAME"].HeaderText = "登录名";
                dataGridView1.Columns["IP"].HeaderText = "IP地址";
                dataGridView1.Columns["MAC_ADDRESS"].HeaderText = "MAC地址";
                dataGridView1.Columns["OCCURE_TIME"].HeaderText = "时间";
                dataGridView1.Columns["SERVER_REMARK"].HeaderText = "信息";
            }
        }

        private void tbMac_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btQueryMac_Click(sender, e);
        }


    }
}
